var selected = -1;
var selectedClass;
var datas = new Array();
var currentPage = 0;
var tableIdentifier = "";

window.onresize = function(event) {
	fillList(currentPage);
}

function createList(table){
	tableIdentifier = table;
	$(table+" tbody tr").each(function(index){
		datas[index]=this;
		$(this).remove();
	});
	$("#list-previous").click(function(){
		previousPageList();
		return false;
	});
	$("#list-next").click(function(){
		nextPageList();
		return false;
	});
	$(table+" tbody tr").live('click',function(){
		selectRow(this);
	});
	fillList(currentPage);
}

function fillList(page) {
	currentPage = page;
	var height = $(".main-bloc").innerHeight()-260;
	var numberOfRow = getInteger(height/30);
	if (numberOfRow==0)
		numberOfRow=1;
	var table = $(tableIdentifier+" tbody");
	$(tableIdentifier+" tbody").html("");
	if (datas.length<=numberOfRow) {
		$(".pagination").css('display','none');
	} else {
		$(".pagination").css('display','block');
	}
	var index = 0;
	var hasElement = false;
	
	for(var i = 0+ numberOfRow*page; i < getMin(numberOfRow*page + numberOfRow,datas.length);i++) {
		hasElement=true;
		if (index%2==0) {
			$(datas[i]).attr('class','even');
		} else {
			$(datas[i]).attr('class','odd');
		}
		index++;
		$(tableIdentifier+" tbody").append(datas[i]);
	}
	if (page>0 && !hasElement) {
		fillList(page-1);
	}
}

/**
 * Set the next page in the list table
 */
function nextPageList() {
	fillList(currentPage+1);
}

/**
 * Set the previous page in the event list table
 */
function previousPageList() {
	if (currentPage>0)
		fillList(currentPage-1);
}

/**
 * this function select a row in a table
 * @param row the row to select
 */
function selectRow(row) {
	if ($(row).hasClass('selected')) {
		$(row).attr('class',selectedClass);
		selected = -1;
	} else {
		$(row).parent().find('tr').each(function(){
			if ($(this).hasClass('selected')) {
				$(this).attr('class',selectedClass);
			}
			
		});
		selectedClass = $(row).attr('class');
		$(row).attr('class','selected');
		selected = $(row).find('[name="id"]').val();
	}
}